package com.k.kpp.logback;

import com.k.kc.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author kivil
 */
@Controller
public class LogController {
    private final static Logger LOGGER = LoggerFactory.getLogger(LogController.class);
    private LogConfigService logConfigService;

    @RequestMapping("/log/test")
    @ResponseBody
    public String log(){
        int count = 100000;
        LOGGER.trace("trace..");
        LOGGER.debug("debug..");
        LOGGER.info("info..");
        LOGGER.warn("warn..");
        LOGGER.error("error..");
        long start = System.currentTimeMillis();
        for(int i=0; i<count;i++){
            LOGGER.info("info.."+i);
            LOGGER.warn("warn.."+i);
            LOGGER.error("error.."+i);
        }
        long end = System.currentTimeMillis();
        //log4j2:22800ms logback:24661
        System.out.println("logback print 100000 time:"+(end-start));
        return "success";
    }

    @RequestMapping("/changeLogDir")
    @ResponseBody
    public String changeLogDir(LogConfigVo vo){
        String dir = vo.getLogSaveDir();
        FileUtils.formatDir(dir);
        logConfigService.update(vo);
        return "success";
    }

    @Autowired
    public void setLogConfigService(LogConfigService logConfigService){
        this.logConfigService = logConfigService;
    }
}
